package org.bapoint.reactadmin.controller;


import org.bapoint.reactadmin.entity.vo.AjaxResult;
import org.bapoint.reactadmin.entity.vo.AjaxSuccess;
import org.bapoint.reactadmin.exception.AppException;

public class RestBase {
    /**
     * return common format error code for AJAX calls
     *
     * @param errorCode client side error code
     */
    protected AjaxResult<String> ajaxErrorCode(String errorCode) {
        AjaxResult<String> ae = new AjaxResult<>();
        ae.setCode(errorCode);
        return ae;
    }

    /**
     * return common format error for AJAX calls
     * <p>
     * this method will directly return error message to client side, client side js does not need to register error to $app.errors
     */
    protected AjaxResult<String>  ajaxError(String error) {
        AjaxResult<String> ae = new AjaxResult<>();
        ae.setMessage(error);
        return ae;
    }

    protected AjaxResult<String>  ajaxError(String code,String msg) {
        AjaxResult<String> ae = new AjaxResult<>();
        ae.setCode(code);
        ae.setMessage(msg);
        return ae;
    }



    /**
     * simply return a success notification with extra data.
     */
    protected <T>  AjaxResult<T> ajaxSuccess(T data,T paginationParam) {
        AjaxResult<T> result = new AjaxResult<>();

        result.setData(data);
        result.setTotal(paginationParam);
        return result;
    }

    protected <T>  AjaxResult<T> ajaxSuccess(T data,String code) {
        AjaxResult<T> result = new AjaxResult<>();

        result.setData(data);
        result.setCode(code);
        return result;
    }

    protected <T>  AjaxResult<T> ajaxSuccess(String code ,String msg) {
        AjaxResult<T> result = new AjaxResult<>();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }
    /**
     * simply return a success notification with extra data.
     */
    protected <T>  AjaxResult<T> ajaxSuccess(T data,String code,String message) {
        AjaxResult<T> result = new AjaxResult<>();

        result.setData(data);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * simply return a success notification without any extra data
     */
    protected AjaxSuccess ajaxSuccess() {
        AjaxSuccess as = new AjaxSuccess();
        as.setReturnCode(1);
        return as;
    }
}